package com.blog.wl2023.aspect;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import com.blog.wl2023.constant.Code;
import com.blog.wl2023.entity.response.Result;
import com.blog.wl2023.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wule
 * @data 2023年08月15日19时
 * @description
 */
@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {

	/**
	 * 默认的全局异常处理,其他异常再添加
	 *
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Result exception(Exception e) {
		log.error("发生全局异常，exception = {}", e.getMessage(), e);
		return Result.internalFail(e.getMessage());
	}

	/**
	 * 处理自定义异常类，业务异常
	 *
	 * @param e
	 * @return
	 */
	@ExceptionHandler(CustomException.class)
	@ResponseStatus(HttpStatus.OK)
	public Result handleCustomException(CustomException e) {
		log.error("发生自定义异常，exception = {},{}", e.getMessage(), e);
		Code code = e.getCode();
		return Result.fail(code.getCode(), code.getMessage());
	}


	/**
	 * 拦截未登录
	 * @param e
	 * @return
	 */
	@ExceptionHandler(NotLoginException.class)
	public Result handlerLoginException(NotLoginException  e) {
		log.error("未登录异常，exception = {},{}", e.getMessage(), e.getCode());
		return Result.fail(-1, "无权限,请登录相应账号!");
	}






}

